import { Component, OnInit } from '@angular/core';
import { JsonResult } from 'src/app/global/model';
import { BillDeliver, BillDeliverService, XMD } from 'src/app/service/bill-deliver.service';
import { NzNotificationService } from 'ng-zorro-antd/notification';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-bill-deliver',
  templateUrl: './bill-deliver.component.html',
  styleUrls: ['./bill-deliver.component.css']
})
export class BillDeliverComponent implements OnInit {

  // 发货单数组数据（主表+明细表构成的数组）
  fhdDetails:BillDeliver[] = [];
  xmd:XMD[] = [];
  mainId:number;
  xmdUrl:string = "https://www.jzy.world/whiteyc/template/send_dtl/";
  testUrl:string = "";
  code:string = "";
  constructor(
    public service:BillDeliverService,
    private noti:NzNotificationService,
    private route:ActivatedRoute
  ) { 
    const aa:string = this.route.snapshot.queryParams["code"];
    this.msg('constructor code=' + aa);
  }

  ngOnInit(): void {
    this.initParams();// 初始化获取发货单主键值
    this.getFHD();// 查询获取发货单单据数据
    this.getXMD();
  }

  // 从地址栏中获取参数
  initParams(){

    this.route.queryParams.subscribe(
      p => {
        this.mainId = p['mainId'];
        this.code = p['code'];

        this.msg('全局code=' + this.code);
        const localCode:string = this.route.snapshot.queryParams["code"];
        this.msg('第二个code=' + localCode);
        this.getUrl();
      }
    )
  }

  getFHD(){
    this.service.fhd(this.mainId).subscribe(
      (jr:JsonResult) => {
        if( jr.code !=1 ){
          this.msg(jr.msg);
          return
        }

        // 将数据赋值到本地变量上
        this.fhdDetails = [...jr.data];

      },error => {
        console.log('向后端请求发货单数据时出现异常！');
        this.msg('向后端请求发货单数据时出现异常！');
      }
    )
  }

  getXMD(){
    this.xmdUrl = this.xmdUrl + this.mainId;
    this.service.xmd().subscribe(
      (jr:JsonResult) =>{
        if( jr.code !=1 ){
          this.msg(jr.msg);
          return
        }

        this.xmd = [...jr.data];
      },error => {
        console.log('请求细码单数据时出现异常');
        this.msg('请求细码单数据时出现异常')
      }
    )
  }


  getUrl(){
    this.service.getXmdUrl(this.code).subscribe(
      (jr:JsonResult) => {
        if( jr.code !=1 ){
          this.msg(jr.msg);
          return
        }

        this.testUrl = jr.data;
      },error => {
        console.log('根据code获取细码单请求 url 时出现异常');
        this.msg('根据code获取细码单请求 url 时出现异常,code=' + this.code);
      }
    )
  }

  msg(content: string) {
    this.noti
    .blank('通知', content)
    .onClick.subscribe(() => {
      console.log('notification clicked!');
    });
  }



}
